//
//  ViewController.m
//  CALayerDemo
//
//  Created by YYPan on 2018/5/18.
//  Copyright © 2018年 YYPan. All rights reserved.
//

#import "ViewController.h"
#import "BezierView.h"
#import <Lottie/Lottie.h>

@interface ViewController ()<CAAnimationDelegate>
@property (nonatomic, strong) CAShapeLayer *dashLineShapeLayer;
@end

@implementation ViewController

- (void)viewDidLoad
{
	[super viewDidLoad];
//	[self CAShaperLayerDemo];
//	[self bezierPathDemo];
//	[self lottieAnimation];
//	[self gradientLayerDemo];
	[self bezierCornerRadius];
}

/**
 lottie动画
 */
- (void)lottieAnimation
{
	LOTAnimationView *view = [LOTAnimationView animationNamed:@"slack_app_loader"];
	view.frame = CGRectMake(0, 0, 200, 200);
	view.center = self.view.center;
	view.loopAnimation = YES;
	[self.view addSubview:view];
	[view play];
}

/**
 贝塞尔曲线demo
 */
- (void)bezierPathDemo
{
	BezierView *view = [[BezierView alloc] initWithFrame:self.view.frame];
	view.backgroundColor = [UIColor whiteColor];
	[self.view addSubview:view];
}

/**
 环形进度条
 */
- (void)gradientLayerDemo
{
	NSMutableArray *colorArray = [[NSMutableArray alloc] init];
	for (NSInteger i = 0; i <= 360; i += 5) {
		UIColor *color = [UIColor colorWithHue:1.0 * i / 360 saturation:1.0 brightness:1.0 alpha:1.0];
		[colorArray addObject:(id)[color CGColor]];
	}
	CAGradientLayer *gradientLayer = [CAGradientLayer layer];
	gradientLayer.frame = CGRectMake(0, 0, 220, 220);
	gradientLayer.position = self.view.center;
	gradientLayer.colors = colorArray;
	gradientLayer.startPoint = CGPointMake(0, 0);
	gradientLayer.endPoint = CGPointMake(1, 0);
	[self.view.layer addSublayer:gradientLayer];
	
	UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 200, 200)];
	CAShapeLayer *layer = [CAShapeLayer layer];
	layer.path = path.CGPath;
	layer.lineWidth = 10.f;
	layer.strokeColor = [UIColor lightGrayColor].CGColor;
	layer.fillColor = [UIColor clearColor].CGColor;
	layer.strokeStart = 0.0f;
	layer.strokeEnd = 0.0f;
	gradientLayer.mask = layer;

	__block CGFloat end = 0.0f;

	[NSTimer scheduledTimerWithTimeInterval:0.2 repeats:YES block:^(NSTimer * _Nonnull timer) {
		end += 0.1f;
		layer.strokeEnd = end;
		if (end >= 1) {
			[timer invalidate];
		}
	}];
}

/**
 虚线
 */
- (void)CAShaperLayerDemo
{
	self.view.backgroundColor = [UIColor blackColor];
	UIBezierPath *dashLinePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 200, 100) cornerRadius:20];
	CAShapeLayer *dashLineShapeLayer = [CAShapeLayer layer];
	dashLineShapeLayer.path = dashLinePath.CGPath;
	dashLineShapeLayer.position = CGPointMake(100, 100);
	dashLineShapeLayer.fillColor = [UIColor clearColor].CGColor;
	dashLineShapeLayer.strokeColor = [UIColor whiteColor].CGColor;
	dashLineShapeLayer.lineWidth = 3;
	dashLineShapeLayer.lineDashPattern = @[@(6),@(6)];
	self.dashLineShapeLayer = dashLineShapeLayer;
	[self.view.layer addSublayer:dashLineShapeLayer];
	[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reload) userInfo:nil repeats:YES];
}

- (void)reload
{
	CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
	animation.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random() % 375, arc4random() % 665)];
	animation.duration = 0.5;
	animation.delegate = self;
	/* 动画结束后不移除动画,这样动画结束后不会回到原来的位置,但是控件的位置并没有改变,移动的只是'显示层' */
	animation.removedOnCompletion = NO;
	animation.fillMode = kCAFillModeForwards;
	[self.dashLineShapeLayer addAnimation:animation forKey:nil];
	NSLog(@"%u",arc4random() % 375);
	NSLog(@"%u",arc4random() % 665);
}

- (void)animationDidStart:(CAAnimation *)anim
{
	NSLog(@"animationDidStart");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
	NSLog(@"%f",self.dashLineShapeLayer.position.x);
	NSLog(@"%f",self.dashLineShapeLayer.position.y);
}


/**
 使用贝塞尔曲线画圆角
 */
- (void)bezierCornerRadius
{
	UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
	imageView.center = self.view.center;
	imageView.image = [UIImage imageNamed:@"qrcode"];
	imageView.contentMode = UIViewContentModeScaleToFill;
	[self.view addSubview:imageView];
	
	UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:80 startAngle:0 endAngle:2 * M_PI clockwise:YES];
	CAShapeLayer *layer = [CAShapeLayer layer];
	layer.path = path.CGPath;
	imageView.layer.mask = layer;
}


@end
